var client = require("../conf/redis").client;
//var logger = require('../conf/log4').logger('crawl');
var rank = require('./rank');
var detail = require('./detail');
var comment = require('./comment');
var cookie = require('./cookie');
var common = require("../conf/common");

function crawlRank(rows) {
    var source = common.getSource();
    for (var i in rows){
        client.lpush("az"+source+"_rank_keyword",JSON.stringify(rows[i]));
    }
    console.log("az"+source+"_rank_keyword done.");
}

function crawlAbsrRank(rows) {
    var source = common.getSource();
    for (var i in rows){
        client.lpush("az"+source+"_absr_rank_keyword",JSON.stringify(rows[i]));
    }
    console.log("az"+source+"_absr_rank_keyword done.");
}
function crawlProDetail(rows,fn) {
    var source = common.getSource();
    for (var i in rows){
        client.lpush("az"+source+"_pro_detail_asin",rows[i].asin);
    }
    console.log("az"+source+"_pro_detail_asin done.");
    if(fn)fn();
}

function loopRankJob(num) {
    var EventProxy = require('eventproxy');
    var source = common.getSource();
    var ep = new EventProxy();

    ep.once('next_word', function (data) {
        ep.removeAllListeners();
        ep = null;
        console.log("Thread["+num+"] word done ----->sort:"+data.category+",keyword:"+data.keyword)
        //刷新一下cookie
        cookie.refresh(num,function () {
            loopRankJob(num);
        });

    });


    client.lpop("az"+source+"_rank_keyword", function (err, res) {
        if(res !== null){
            var obj = JSON.parse(res);
            obj.num = num;
            var i =1;
            rank.loadAzRank(obj,i,ep);
            ep.on("next_page",function () {
                i++;
                rank.loadAzRank(obj,i,ep);
            });
            ep.on("sleep",function (a) {
                setTimeout(function () {
                    rank.loadAzRank(obj,a,ep);
                },3000);
            });
        }else{
            console.log("Thread["+num+"] az "+source+" rank job done ----")
            ep.removeAllListeners();
            ep = null;
            //jobEP.emit('next_job');
            return;
        }
    });
}





function loopAbsrJob(num) {
    var EventProxy = require('eventproxy');
    var source = common.getSource();
    var ep = new EventProxy();

    ep.once('next_word', function (data) {
        ep.removeAllListeners();
        ep = null;
        console.log("Thread["+num+"] next word ----->category:"+data.category+",url:"+data.url)
        loopAbsrJob(num);
    });

    client.lpop("az"+source+"_absr_rank_keyword", function (err, res) {
        if(res !== null){
            var obj = JSON.parse(res);
            obj.num = num;
            rank.loadAzAbsrRank(obj,ep);

            ep.on("sleep",function (errObj) {
                console.log("Thread["+num+"] sleep : 3 secd")
                //console.log(errObj)
                setTimeout(function () {
                    rank.loadAzAbsrRank(errObj,ep);
                },3000);
            });
        }else{
            console.log("Thread["+num+"] az"+source+" absr rank job done ----");
            ep.removeAllListeners();
            ep = null;
            return;
        }
    });
}

function loopProJob(num) {
    var EventProxy = require('eventproxy');
    var ep = new EventProxy();
    var retry = 0;
    var source = common.getSource();

    ep.once('next_asin', function (data) {
        ep.removeAllListeners();
        ep = null;
        loopCommJob(num,data);
    });

    client.lpop("az"+source+"_pro_detail_asin", function (err, res) {
        if(res !== null){
            console.log("Thread["+num+"] next word ----->asin:"+res);
            detail.loadDetail(res,ep,num);

            ep.on("sleep",function (errObj) {
                retry ++;
                console.log("Thread["+num+"] sleep : 3 secd-->retry:"+retry);
                if(retry>2){
                    ep.removeAllListeners();
                    var obj = {
                        asin:res,
                        type:'detail',
                    };
                    client.rpush("az"+source+"_pro_error_asin",JSON.stringify(obj));
                    cookie.refresh(num,function (j) {
                        loopProJob(j);
                    });
                }else{
                    setTimeout(function () {
                        detail.loadDetail(errObj,ep,num);
                    },3000);
                }
            });
        }else{
            console.log("Thread["+num+"] az"+source+"  pro detail job done ----")
            ep.removeAllListeners();
            ep = null;
            cookie.refresh(num,function (j) {
                loopErrorJob(j);
            });
            //return;
        }
    });
}


function loopCommJob(num,asin) {
    var EventProxy = require('eventproxy');
    var ep = new EventProxy();
    var retry = 0;
    var page = 1;
    var source = common.getSource();
    ep.once('next_asin', function (data) {
        ep.removeAllListeners();
        loopProJob(num);
    });

    ep.on('next_page', function (data) {
        comment.loadComment(asin,ep,num,++page);
    });

    comment.loadComment(asin,ep,num,page);

    ep.on("sleep",function (errObj) {
        retry ++;
        console.log("Thread["+num+"] sleep : 3 secd-->retry:"+retry);
        if(retry>2){
            ep.removeAllListeners();
            var obj = {
                asin:asin,
                type:'comment',
            };
            client.rpush("az"+source+"_pro_error_asin",JSON.stringify(obj));
            cookie.refresh(num,function (j) {
                loopProJob(j);
            });

        }else{
            setTimeout(function () {
                comment.loadComment(asin,ep,num,page);
            },3000);
        }
    });
}

//抓取之前错语的asin,重新再抓一次
function loopErrorJob(num) {
    var EventProxy = require('eventproxy');
    var source = common.getSource();
    var ep = new EventProxy();
    var page = 1;
    ep.once('next_asin', function (data) {
        ep.removeAllListeners();
        ep = null;
        loopErrorJob(num);
    });

    ep.on('next_page', function (data) {
        comment.loadComment(asin,ep,num,++page);
    });

    client.lpop("az"+source+"_pro_error_asin", function (err, res) {
        if(res !== null){
            var obj = JSON.parse(res);
            if(obj.type=='detail'){
                detail.loadDetail(obj.asin,ep,num);
            }else{
                comment.loadComment(obj.asin,ep,num,page);
            }

            ep.on("sleep",function (a) {
                setTimeout(function () {
                    if(obj.type=='detail'){
                        detail.loadDetail(obj.asin,ep,num);
                    }else{
                        comment.loadComment(obj.asin,ep,num,page);
                    }
                },3000);
            });
        }else{
            console.log("Thread["+num+"] az "+source+" error job done ----")
            ep.removeAllListeners();
            ep = null;
            return;
        }
    });
}



exports.crawlRank = crawlRank;
exports.crawlAbsrRank = crawlAbsrRank;
exports.crawlProDetail = crawlProDetail;

exports.loopRankJob = loopRankJob;
exports.loopAbsrJob = loopAbsrJob;
exports.loopProJob = loopProJob;
exports.loopCommJob = loopCommJob;
